using System.Collections;
using System.Collections.Generic;
using AppFileTools;
using UnityEngine;

public class BeatRecorder : MonoBehaviour
{
    public List<string> key_ls = new List<string>()
    {
        "A",
        "D"
    };
    private void OnGUI()
    {
        if (GUILayout.Button($"btn_reset_beat_dc"))
        {
            btn_reset_beat_dc_ls();
        }
        if (GUILayout.Button($"Key[{key_ls[0]}]"))
        {
            btn_add_one_beat(0);
            
            btn_show_beat_dc_ls();
        }
        if (GUILayout.Button($"Key[{key_ls[1]}]"))
        {
            btn_add_one_beat(1);
            
            btn_show_beat_dc_ls();
        }
        if (GUILayout.Button($"btn_show_beat_dc"))
        {
            btn_show_beat_dc_ls();
        }
        if (GUILayout.Button($"btn_save_beat_file"))
        {
            btn_save_beat_file();
        }
    }

    List<Dictionary<float, string>> beat_dc_ls;
    private float beat_timer;
    private int timer_index;
    
    [Header("计时器小数点位数")]
    public int timer_digits = 3;

    void btn_reset_beat_dc_ls()
    {
        beat_dc_ls = new List<Dictionary<float, string>>();
        beat_timer = 0;
    }

    [Header("是否打开保存文件路径")]
    public bool is_open_dir = false;

    void btn_save_beat_file()
    {
        beat_file = new FileObject("beat_file.json", "archives/custom");
        beat_file.content_dc.Clear();
        beat_file.content_dc.Add("beat_dc_ls", beat_dc_ls);
        beat_file.save();
        if (is_open_dir)
        {
            beat_file.open_dir(is_persistent_dir: true);
        }
    }
    
    void btn_add_one_beat(int key_i)
    {
        string key_v = key_ls[key_i];

        var _dc = new Dictionary<float, string>(){{BdPackage.BdUtils.round(beat_timer, timer_digits), key_v}};
        beat_dc_ls.Add(_dc);
    }

    public void btn_show_beat_dc_ls()
    {
        BdPackage.BdUtils.log("--- beat_dc_ls:", beat_dc_ls);
    }

    private FileObject beat_file;

    // Start is called before the first frame update
    void Start()
    {
        btn_reset_beat_dc_ls();
    }

    // Update is called once per frame
    void Update()
    {
        beat_timer += Time.deltaTime;

        var keyCode_1 = (KeyCode)System.Enum.Parse(typeof(KeyCode), key_ls[0]);
        if (Input.GetKeyDown(keyCode_1))
        {
            BdPackage.BdUtils.log($"--- press 0 [{keyCode_1}]!");
        }
        
        keyCode_1 = (KeyCode)System.Enum.Parse(typeof(KeyCode), key_ls[1]);
        if (Input.GetKeyDown(keyCode_1))
        {
            BdPackage.BdUtils.log($"--- press 1 [{keyCode_1}]!");
        }
    }
}
